﻿package net.wangit.framework.action;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.wangit.context.ActionContextHelper;
import net.wangit.context.LocalContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


@SuppressWarnings("serial")
public class DispatcherActionResult extends AbstractActionResult {

	private static final Log log = LogFactory
			.getLog(net.wangit.framework.action.DispatcherActionResult.class);

	public DispatcherActionResult() {
	}

	public void execute() throws Exception {
		if (log.isDebugEnabled())
			log.debug((new StringBuilder("Forwarding to location "))
					.append(url).toString());
		LocalContext lc = LocalContext.getLocalContext();
		HttpServletRequest request = lc.getHttpServletRequest();
		HttpServletResponse response = lc.getHttpServletResponse();
		RequestDispatcher dispatcher = request.getRequestDispatcher(url);
		if (dispatcher == null) {
			response.sendError(404, (new StringBuilder("result '")).append(url)
					.append("' not found").toString());
			return;
		} else {
			ActionContextHelper.update();
			dispatcher.forward(request, response);
			return;
		}
	}

}
